package com.situ.company.department.service;

import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.dao.Inf.Department2Dao;
import com.situ.company.department.dao.Inf.DepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.employee.dao.EmployeeDao;
import com.situ.company.employee.model.EmployeeModel;

public class DepartmentService {

//	private DepartmentDao dao = new DepartmentDao();
	private IDepartmentDao dao = new DepartmentDao();
	private EmployeeDao employeeDao = new EmployeeDao();

	public String insert(DepartmentModel model) {
		DepartmentModel mod = selectModel(model);
		if (mod != null) {
			return "E";
		}
		Integer result = dao.insert(model);
		return String.valueOf(result);
	}

	public String delete(DepartmentModel model) {
		EmployeeModel m0 = new EmployeeModel();
		m0.setDepCode(model.getCode());
		int count = employeeDao.selectCount(m0);
		if(count > 0) {
			return "E";
		}
		Integer result = dao.delete(model);
		return String.valueOf(result);
	}

	public String update(DepartmentModel model) {
		Integer result = dao.update(model);
		return String.valueOf(result);
	}

	/**
	 * 多条件+模糊查询 --一些记录
	 * 
	 * @param model
	 * @return
	 */
	public List<DepartmentModel> selectList(DepartmentModel model) {
		String code = model.getCode();
		System.out.println(code);
		if (code != null) {
			model.setCode("%" + code + "%");
		}
		String name = model.getName();
		System.out.println(name);
		if (name != null) {
			model.setName("%" + name + "%");
		}
		return dao.selectList(model);
	}

	/**
	 * 根据(逻辑)主键查询出唯一记录，主键不存在返回null
	 * 
	 * @param model
	 * @return
	 */
	public DepartmentModel selectModel(DepartmentModel model) {
		DepartmentModel temp = new DepartmentModel();
		temp.setCode(model.getCode());
		List<DepartmentModel> list = dao.selectList(temp);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 根据多条件的模糊查询出的记录条数
	 * 
	 * @param model
	 * @return
	 */
	public int selectCount(DepartmentModel model) {
		DepartmentModel temp = new DepartmentModel();
		String code = model.getCode();
		temp.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		temp.setName(name == null ? "%%" : "%" + name + "%");
		temp.setPageOn(false);
		return dao.selectCount(temp);
	}

}
